<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi, nên tạo 1 dataaccesslayer ket noi thao tac du lieu hay hon
include ('clsketnoi.php');

function taomatacgia()
{
    return taomatudong("select matg from tbltacgia","matg",2,15);
}

//them du lieu
function themtacgia()
{
    $matg = $_POST['matg'];
    $hoten = $_POST['tentg'];
    $gioitinh = $_POST['gioitinh'];
    $luocsu = $_POST['luocsu'];
    $query = mysql_query("insert into tbltacgia (matg, hoten, gioitinh, luocsu) values ('$matg','$hoten','$gioitinh','$luocsu')",dbketnoi());
    //giai phong bo nho
    mysql_free_result($query);
    mysql_close();
}
//xoa tac gia
function xoatacgia()
{
    $matg= $_POST['matg'];
    $query = mysql_query("select madausach, matg from tbldausach",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['matg']==$matg)
        {
            mysql_query("delete from tbldausach where madausach='".$result['madausach']."'", dbketnoi());
        }
        $i++;
    }
    
    mysql_query("delete from tbltacgia where matg='$matg'",dbketnoi());
    mysql_close();
}
//update tac gia
function updatetacgia()
{
    $matg = $_POST['matg'];
    $hoten = $_POST['tentg'];
    $gioitinh = $_POST['gioitinh'];
    $luocsu = $_POST['luocsu'];
    mysql_query("update tbltacgia set hoten='$hoten', gioitinh='$gioitinh', luocsu='$luocsu' where matg='$matg'",dbketnoi());
    mysql_close();
}

// hien thi du lieu
function hienthitacgia()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT matg, hoten, gioitinh, luocsu from tbltacgia",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodientacgia.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><input type='image' src='images/editIcon.png' name='edit' id='edit' title='Edit' onclick='<?php  ?>'/></td>";
            echo "<td><a href=''>".$result['matg']."</a></td>";
            echo "<td>".$result['hoten']."</td>";
            echo "<td>".$result['gioitinh']."</td>";
            echo "<td>".$result['luocsu']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("   ","Mã Tác Giả ","Tên Tác Giả","Giới Tính","Lược sử");
    echo "<table style='border-color:black;'  cellspacing='1' width='500px'>";
    echo "<tr>";
    for($i=0; $i<=4; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}
function hienthitext()
{
    $matg = $_POST['matg'];
    $query = mysql_query("select matg, hoten, gioitinh, luocsu from tbltacgia where matg='".$matg."'",dbketnoi());
    $row = mysql_fetch_array($query);

}

?>
